package com.example.livedatautilsdemo.utils;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;

import com.example.livedatautilsdemo.receiver.UnInstallResultReceiver;

/**
 * @author: njb
 * @date: 2023/8/9 9:43
 * @desc:
 */
public class InstallUtils {
    private static final String PACKAGE_UNINSTALLED_ACTION =
            "com.xxx.uninstall";

    /**
     * 根据包名卸载应用
     * @param packageName
     */
    public static void uninstall(String packageName, Context context) {
        Intent broadcastIntent = new Intent(context, UnInstallResultReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
                broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
        packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());
    }

    /**
     * 根据包名卸载应用
     *
     * @param packageName
     */
    public void uninstall(Context context,String packageName) {
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(PACKAGE_UNINSTALLED_ACTION);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
        packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());

    }

}
